﻿<ContentControl x:Class="Microsoft.VisualStudio.TestTools.WorkItemMigrator.DataMappingView"
                                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                                 xmlns:WorkItemMigrator="clr-namespace:Microsoft.VisualStudio.TestTools.WorkItemMigrator"
                                 x:ClassModifier="internal">
    <ContentControl.Resources>
        <WorkItemMigrator:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
        <WorkItemMigrator:InverseBooleanToVisibilityConverter x:Key="InverseBooleanToVisibilityConverter" />

        <Style x:Key="VerticalSeparator"
               TargetType="{x:Type Path}">
            <Setter Property="Data"
                Value="M 0,0 L 0,24" />
            <Setter Property="Stretch"
                Value="None" />
            <Setter Property="Stroke"
                Value="Gray" />
            <Setter Property="VerticalAlignment"
                Value="Center" />
            <Setter Property="Margin"
                Value="0,0,2,0" />
            <Setter Property="SnapsToDevicePixels"
                Value="True" />
        </Style>

        <Style x:Key="DataMappingListBoxItem"
           TargetType="{x:Type ListBoxItem}">
            <Setter Property="OverridesDefaultStyle"
                Value="True" />
            <Setter Property="Background"
                Value="Transparent" />
            <Setter Property="HorizontalContentAlignment"
                Value="Stretch" />
            <Setter Property="VerticalContentAlignment"
                Value="Center" />
            <Setter Property="KeyboardNavigation.DirectionalNavigation"
                Value="None" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border Name="Bd"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                SnapsToDevicePixels="true"
                                CornerRadius="2"
                                Focusable="False">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                          Focusable="False"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected"
                                 Value="true">
                                <Setter TargetName="Bd"
                                    Property="Background"
                                    Value="{DynamicResource ItemBackgroundBrush}" />
                            </Trigger>
                            <Trigger Property="IsEnabled"
                                 Value="false">
                                <Setter Property="Foreground"
                                    Value="Gray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ContentControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListBox ItemsSource="{Binding DataMappingRows}"
                 SelectionMode="Extended"     
                 Grid.Row="0"
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                 ItemContainerStyle="{StaticResource DataMappingListBoxItem}">
            <ListBox.Resources>
                <Style TargetType="{x:Type TextBlock}"
                       x:Key="TextBlockStyle">
                    <Setter Property="Foreground" Value="Black" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEnabled}"
                                     Value="False">
                            <Setter Property="Foreground" Value="Gray" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>

                <DataTemplate DataType="{x:Type WorkItemMigrator:DataMappingRow}">
                    <Border HorizontalAlignment="Stretch"
                            BorderBrush="Gray"
                            BorderThickness="0.5"
                            SnapsToDevicePixels="true"
                            Focusable="False">
                        <Grid IsEnabled="{Binding IsEnabled}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding DataSourceField}"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       Grid.Column="0"
                                       Visibility="{Binding IsEditable, Converter={StaticResource InverseBooleanToVisibilityConverter}}">
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}"
                                           BasedOn="{StaticResource TextBlockStyle}">
                                        <Setter Property="Foreground" Value="Black" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}"
                                                         Value="True">
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                            <Grid Grid.Column="0"
                                  Visibility="{Binding IsEditable, Converter={StaticResource BooleanToVisibilityConverter}}">
                                <TextBlock Text="{Binding DataSourceField}"
                                           TextWrapping="Wrap"
                                           VerticalAlignment="Center"
                                           Style="{StaticResource TextBlockStyle}"
                                            Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource InverseBooleanToVisibilityConverter}}" />

                                <ComboBox SelectedItem="{Binding DataSourceField}"
                                          VerticalAlignment="Top"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource BooleanToVisibilityConverter}}"
                                          ItemsSource="{Binding DataSourceFields}" />
                            </Grid>

                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="1" />

                            <TextBlock Text="{Binding DataSourceValue}"
                                       TextWrapping="Wrap"
                                       Visibility="{Binding IsEditable, Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                       Grid.Column="2" >
                                <TextBlock.Style>
                                    <Style TargetType="{x:Type TextBlock}"
                                           BasedOn="{StaticResource TextBlockStyle}">
                                        <Setter Property="Foreground" Value="Black" />
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path=IsSelected}"
                                                         Value="True">
                                                <Setter Property="Foreground" Value="White" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>

                            <TextBox Text="{Binding DataSourceValue}"
                                       VerticalAlignment="Center"
                                       Visibility="{Binding IsEditable, Converter={StaticResource BooleanToVisibilityConverter}}"
                                       Grid.Column="2"
                                       ToolTip="{Binding DataSourceValue}" />

                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="3" />

                            <Grid Grid.Column="4">
                                <TextBlock Text="{Binding NewValue}"
                                           TextWrapping="Wrap"
                                           Style="{StaticResource TextBlockStyle}"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource InverseBooleanToVisibilityConverter}}"
                                            VerticalAlignment="Center" />
                                <ComboBox x:Name="WIFieldComboBox"
                                          SelectedItem="{Binding NewValue}"
                                          VerticalAlignment="Center"
                                          Visibility="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}},
                                                               Converter={StaticResource BooleanToVisibilityConverter}}"
                                          ItemsSource="{Binding AllowedNewValues}" />
                            </Grid>
                        </Grid>
                    </Border>
                </DataTemplate>

                <DataTemplate DataType="{x:Type WorkItemMigrator:BlankDataMappingRow}">
                    <Border>
                        <Button Content="Add"
                                Margin="0,5,0,0"
                                MinWidth="40"
                                HorizontalAlignment="Left"
                                Click="AddDataMappingRowButton_Click" />
                    </Border>
                </DataTemplate>

                <DataTemplate DataType="{x:Type WorkItemMigrator:HeaderDataMappingRow}">
                    <Border HorizontalAlignment="Stretch"
                            BorderBrush="Gray"
                            BorderThickness="0.5"
                            SnapsToDevicePixels="true"
                            Padding="0,3,0,3"
                            Focusable="False">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="{Binding DataSourceField}"
                                       FontWeight="Bold"
                                       Grid.Column="0"
                                       Padding="3"/>
                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="1" />

                            <TextBlock Text="{Binding DataSourceValue}"
                                       FontWeight="Bold"
                                       Grid.Column="2"
                                       Padding="3"/>

                            <Path Style="{StaticResource VerticalSeparator}"
                                  Grid.Column="3" />

                            <TextBlock Text="{Binding NewValue}"
                                       FontWeight="Bold"
                                       Grid.Column="4"
                                       Padding="3"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ListBox.Resources>
        </ListBox>

        <CheckBox Grid.Row="1"
                  IsChecked="{Binding CreateAreaIterationPath}"
                  Margin="0,5,0,3"
                  Visibility="{Binding IsCreateAreaIterationPathVisible, Converter={StaticResource BooleanToVisibilityConverter}}">
            Create area/iteration path from source values
        </CheckBox>
    </Grid>
</ContentControl>
